<?php
/**
 * Created by PhpStorm.
 * User: beijingwhg
 * Date: 2017/5/15
 * Time: 下午6:03
 */

namespace App\Http\Controllers\Home;


use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;

class AjaxController extends Controller
{
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function upload()
    {
        $updateType = $this->request->get('type');

        if ($updateType != 'avatar' && $updateType != 'poster') {
            return $this->ajaxReturn(10001);
        }

        $imgUrl = $this->request->get('imgUrl');
        $imgInitW = $this->request->get('imgInitW');
        $imgInitH = $this->request->get('imgInitH');
        $imgW = $this->request->get('imgW');
        $imgH = $this->request->get('imgH');
        $imgY1 = $this->request->get('imgY1');
        $imgX1 = $this->request->get('imgX1');
        $cropW = $this->request->get('cropW');
        $cropH = $this->request->get('cropH');
        $angle = $this->request->get('rotation');
        $jpeg_quality = 100;
        $date = Carbon::now()->toDateString();
        $output_filename = config('self.default-'.$updateType.'-path').$date.'/';

        if (!file_exists($output_filename)) {
            mkdir($output_filename);
        }

        $output_filename .= uniqid($updateType);

        $what = getimagesize($imgUrl);

        switch(strtolower($what['mime']))
        {
            case 'image/png':
                $img_r = imagecreatefrompng($imgUrl);
                $source_image = imagecreatefrompng($imgUrl);
                $type = '.png';
                break;
            case 'image/jpeg':
                $img_r = imagecreatefromjpeg($imgUrl);
                $source_image = imagecreatefromjpeg($imgUrl);
                error_log("jpg");
                $type = '.jpeg';
                break;
            case 'image/gif':
                $img_r = imagecreatefromgif($imgUrl);
                $source_image = imagecreatefromgif($imgUrl);
                $type = '.gif';
                break;
            default: die('image type not supported');
        }

        $resizedImage = imagecreatetruecolor($imgW, $imgH);
        imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
        $rotated_image = imagerotate($resizedImage, -$angle, 0);
        $rotated_width = imagesx($rotated_image);
        $rotated_height = imagesy($rotated_image);
        $dx = $rotated_width - $imgW;
        $dy = $rotated_height - $imgH;
        $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
        imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
        imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
        $final_image = imagecreatetruecolor($cropW, $cropH);
        imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
        imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
        imagejpeg($final_image, $output_filename.$type, $jpeg_quality);

        $this->save([$updateType=>'/'.$output_filename.$type]);

        return $this->ajaxReturn(10000, '/'.$output_filename.$type);
    }

    private function ajaxReturn($code = 0, $data = [])
    {
        return response()->json([
            'code'  =>  $code,
            'msg'   =>  config('error.'.$code),
            'data'  =>  $data
        ]);
    }

    private function save($data)
    {
        return user()->update($data);
    }
}